//
//  AbstractNavigationController.m
//  MISCloud
//
//  Created by goldeney on 13-8-16.
//  Copyright (c) 2013年 goldeneye. All rights reserved.
//

#import "AbstractNavigationController.h"
#import "PreviewLocalFileTypeViewController.h"
#import "FilePathHelper.h"
//#import "UINavigationItem+margin.h"
#import "UploadViewController.h"

@interface AbstractNavigationController()<UIGestureRecognizerDelegate, UINavigationControllerDelegate>

@end

@implementation AbstractNavigationController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self.navigationBar setTintColor:[UIColor whiteColor]];
        //self.navigationBar.translucent = NO;
        [self.navigationBar setShadowImage:[[UIImage alloc] init]];
        [self setNavigationBarHidden:NO];
        [self.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                    [UIColor whiteColor], NSForegroundColorAttributeName,
                                                    [UIFont boldSystemFontOfSize:18.0], NSFontAttributeName, nil]];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.delegate = self;
    
    __weak typeof (self) weakSelf = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = weakSelf;
    }
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // fix 'nested pop animation can result in corrupted navigation bar'
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    
    [super pushViewController:viewController animated:animated];
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    if ([self.topViewController isKindOfClass:[UploadViewController class]]) {
        //不立即删除“文件上传模块”中的文件，待下次操作删除
        return [super popViewControllerAnimated:animated];
        
    } else {
        UIViewController *viewController = [super popViewControllerAnimated:animated];
        //判断是否是缓存"照片或视频文件"目录，如果存在就删除里面数据
        if (viewController && ![viewController isKindOfClass:[PreviewLocalFileTypeViewController class]]) {
            [self deleteCachePhotosFolderPathFile];
        }
        return viewController;
    }
}

- (void)deleteCachePhotosFolderPathFile
{
    //判断是否是缓存"照片或视频文件"目录，如果存在就删除里面数据
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = FALSE;
    NSError *error = nil;
    NSString *filePah = [FilePathHelper getCachePhotosFolderPathNotCreat];
    
    //判断文件夹是否存在
    BOOL isDirExist = [fileManager fileExistsAtPath:filePah isDirectory:&isDir];
    if(isDirExist && isDir){
        BOOL isDelete = [fileManager removeItemAtPath:filePah error:&error];
        NSLog(@"==删除缓存照片文件==%d",isDelete);
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark UIGestureRecognizerDelegate

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    //关闭主界面的右滑返回
    if (self.viewControllers.count <= 1) {
        return NO;
    } else {
        return YES;
    }
}

#pragma mark UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}

@end
